package org.eredlab.g4.ccl.id.generator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eredlab.g4.arm.util.idgenerator.IDHelper;
import org.eredlab.g4.ccl.id.CreateIDException;
import org.eredlab.g4.ccl.id.IDGenerator;
import org.eredlab.g4.ccl.id.PrefixGenerator;
import org.eredlab.g4.ccl.id.SequenceFormater;
import org.eredlab.g4.ccl.id.SequenceGenerator;
import org.eredlab.g4.ccl.id.sequence.DefaultSequenceGenerator;

/**
 * DefaultIDGenerator 此代码源于开源项目E3,原作者：黄云辉
 * 
 * @author XiongChun
 * @since 2010-03-17
 * @see IDGenerator
 */
public class DefaultIDGenerator implements IDGenerator {

	private PrefixGenerator prefixGenerator;
	private SequenceGenerator sequenceGenerator = new DefaultSequenceGenerator();
	private SequenceFormater sequenceFormater;

	private final Log logger = LogFactory.getLog(DefaultIDGenerator.class);

	public synchronized String create() throws CreateIDException {
		final String prefix = prefixGenerator == null ? "" : prefixGenerator.create();
		logger.debug("ID前缀是:[" + prefix + "]");
		long sequence = sequenceGenerator.next();
		final String strSequence = sequenceFormater == null ? new Long(sequence).toString() : sequenceFormater
				.format(sequence);
		return prefix + strSequence;
	}

	public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
		this.prefixGenerator = prefixGenerator;
	}

	public void setSequenceGenerator(SequenceGenerator sequenceGenerator) {
		this.sequenceGenerator = sequenceGenerator;
	}

	public void setSequenceFormater(SequenceFormater sequenceFormater) {
		this.sequenceFormater = sequenceFormater;
	}
	
	@Override
	public String create(String prefix,int index) throws CreateIDException {
		prefix = prefix == null ? "" : prefix.toUpperCase().trim();
		String retu = "";
		logger.debug("ID前缀是:[" + prefix + "]");
		long sequence = sequenceGenerator.next();
		String strSequence = sequenceFormater == null ? new Long(sequence).toString() : sequenceFormater
				.format(sequence);
		if(index == 0){//最开头
			retu = prefix + strSequence.trim();
		}else if(index == -1){//最结尾
			retu =  strSequence.trim().concat(prefix);
		}else{
			if(strSequence.length()> index){
				String new1 = strSequence.substring(0,index);
				String new2 = strSequence.substring(index, strSequence.length());
				retu = new1.trim().concat(prefix).concat(new2.trim());
				//String[] sub = strSequence.
			}else{
				retu =  (strSequence +prefix).trim();
			}
		}
		return retu.trim();
	}
	
	

}
